package com.sz.controller;

import com.alibaba.fastjson.JSON;
import com.sz.model.Result;
import com.sz.model.TUser;
import com.sz.myinterface.SMSCode;
import com.sz.service.IUserService;
import com.sz.utils.JwtUtil;
import com.sz.utils.Md5Util;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/tourist")
//@CrossOrigin(origins = "*", maxAge = 3600)
public class UserController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private Md5Util md5Util;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/login")
    public Result queryUkPhone(@Param("ukPhone") String ukPhone, @Param("userPassword") String userPassword) throws Exception {
        System.out.println(ukPhone + "---------------------");

        //将用户的密码进行加密
        String userpassword = userPassword;
        String password = iUserService.queryUserPassword(ukPhone);
        boolean md5 = md5Util.passwordEqualMD5(userpassword, password);
//        boolean md5=(userPassword.equals(password));

        Result result = new Result();
        Map<String, TUser> map = new HashMap<>();
        if (md5) {
            //调用登录方法
            TUser user = iUserService.queryUkPhone(ukPhone);
            System.out.println("user.toString() = " + user.toString());
            //如果登录的用户存在的话，生成token
            if (user.getStatus() == 0) {
                if (user != null) {
                    String subject = JSON.toJSONString(user);
                    String id = String.valueOf(user.getPkUserId());
                    //生成token
                    String token = jwtUtil.createJwt(id, ukPhone, subject, "user");
                    stringRedisTemplate.opsForValue().set("token", token, 1800, TimeUnit.SECONDS);
                    //(user.getUkPhone(), user.getPkUserId());
                    if (token != null) {
                        System.out.println("token---------------" + token);

                        map.put(token, user);
                        result = new Result(200, "登录成功", map);
                    } else {
                        result.setCode(301);
                        result.setMsg("登录失败");
                    }
                    return result;
                } else {
                    System.out.println("账户不存在");
                    result = new Result(301, "登录失败，账户不存在");
                }
            } else {
                result = new Result(308, "账户已冻结");
            }

        } else {
            System.out.println("密码错误");
            result = new Result(303, "登录失败，密码错误");
        }
        return result;
    }

    @PostMapping("/register")
    public Result addUser(String code, @RequestBody TUser tUser) throws Exception {
        //将密码通过MD5进行加密，再存进去
        System.out.println("code==="+code+"-----"+tUser.toString());
        tUser.setUserPassword(md5Util.getSaltMD5(tUser.getUserPassword()));
        tUser.setAcountPassword(md5Util.getSaltMD5(tUser.getAcountPassword()));
        Byte b=0;
        tUser.setStatus(b);

        tUser.setRegDate(new Date());
        tUser.setRegAddress("深圳");
        Result result = null;
        String sms = yanZheng();
        System.out.println("------------------"+tUser.toString());
        //注册账号
        if (iUserService.queryUkPhone(tUser.getUkPhone()) == null) {
            int register = iUserService.addUser(tUser);

            if (register > 0) {

                result=new Result(200,"注册成功");

            } else {
                result=new Result(401,"注册失败");
            }
        } else {
            result=new Result(305,"用户名重复");
        }
        return result;
    }

    @GetMapping("/yanzheng")
    public String yanZheng() {
        TUser tUser = new TUser();
        String sms = SMSCode.sendSMS(tUser.getUkPhone());
        return sms;
    }

    @RequestMapping(value = "/token",method = RequestMethod.POST)
    public Result getToken(){
        Result result=null;

        String token = stringRedisTemplate.opsForValue().get("token");

        if(token==null||token==""){
            result=new Result(201,"登录已过期");
        }else{
            result=new Result(202,"已登录");
        }

        return result;
    }
}
